package com.binya.lib.permission;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.binya.lib.permission.inter.PermissionDeniedComparator;
import com.binya.lib.permission.inter.PermissionResultComparator;
import com.binya.lib.permission.pop.PermissionAuthReminderPop;

import java.util.List;

/**
 * @author ydh
 * Time 2023年06月05日
 */
public class ByPermissionHelper {

    private static boolean isExitStatus = false;
    private static long mExitTime;
    private static long mExitTimeMillis = 350;

    /**
     * 客服所需权限 申请及判断
     *
     * @param context
     * @param view
     * @param comparable
     */
    @SuppressLint("WrongConstant")
    public static void jumpPermissionChat(Activity context, View view, Comparable<String> comparable) {
        if (ByPermissionTool.isPermission(context, ByPermissionConfig.permissionStorageAndCameraAndPhoneAndAudio)) {
            comparable.compareTo("");
            return;
        }
        jumpPermission(context, ByPermissionConfig.permissionStorageAndCameraAndPhoneAndAudioCode,
                ByPermissionConfig.permissionStorageAndCameraAndPhoneAndAudio, view, new Comparable<String>() {
                    @Override
                    public int compareTo(String s) {
                        comparable.compareTo("");
                        return 0;
                    }
                });
//        jumpPermission(context, ByPermissionConfig.permissionStorageCode, ByPermissionConfig.permissionStorage, view, new Comparable<String>() {
//            @Override
//            public int compareTo(String s) {
//                jumpPermission(context, ByPermissionConfig.permissionPhoneCode, ByPermissionConfig.permissionPhone, view, new Comparable<String>() {
//                    @Override
//                    public int compareTo(String s) {
//                        jumpPermission(context, ByPermissionConfig.permissionAudioCode, ByPermissionConfig.permissionAudio, view, new Comparable<String>() {
//                            @Override
//                            public int compareTo(String s) {
//                                jumpPermission(context, ByPermissionConfig.permissionCameraCode, ByPermissionConfig.permissionCamera, view, new Comparable<String>() {
//                                    @Override
//                                    public int compareTo(String s) {
//                                        comparable.compareTo("");
//                                        return 0;
//                                    }
//                                });
//                                return 0;
//                            }
//                        });
//                        return 0;
//                    }
//                });
//                return 0;
//            }
//        });
    }

    public static void jumpPermissionChatResult(Activity context, int requestCode, String[] permissions, int[] grantResults, View view, Comparable<String> comparable) {
        jumpPermissionChatResult(context, requestCode, permissions, grantResults, view, comparable, null);
    }

    public static void jumpPermissionChatResult(Activity context, int requestCode, String[] permissions, int[] grantResults, View view, Comparable<String> comparable, Comparable<String> comparableClear) {
        dismissTipPop();
        if (ByPermissionTool.isPermission(context, ByPermissionConfig.permissionStorageAndCameraAndPhoneAndAudio)) {
            ByPermissionTool.onRequestPermissionsResult(context, permissions, grantResults, null);
            comparable.compareTo("");
            return;
        }
        if (comparableClear != null) {
            comparableClear.compareTo("");
        }
        dialogTip(context, permissions, grantResults);
//        ByPermissionTool.onRequestPermissionsResult(context, permissions, grantResults, new ByPermissionTool.PermissionCallback() {
//            @Override
//            public void onPermissionsGranted(@NonNull List<String> perms) {
//                if (requestCode == ByPermissionConfig.permissionStorageCode) {
//                    jumpPermission(context, ByPermissionConfig.permissionPhoneCode, ByPermissionConfig.permissionPhone, view, new Comparable<String>() {
//                        @Override
//                        public int compareTo(String s) {
//                            jumpPermission(context, ByPermissionConfig.permissionAudioCode, ByPermissionConfig.permissionAudio, view, new Comparable<String>() {
//                                @Override
//                                public int compareTo(String s) {
//                                    jumpPermission(context, ByPermissionConfig.permissionCameraCode, ByPermissionConfig.permissionCamera, view, new Comparable<String>() {
//                                        @Override
//                                        public int compareTo(String s) {
//                                            comparable.compareTo("");
//                                            return 0;
//                                        }
//                                    });
//                                    return 0;
//                                }
//                            });
//                            return 0;
//                        }
//                    });
//                } else if (requestCode == ByPermissionConfig.permissionPhoneCode) {
//                    jumpPermission(context, ByPermissionConfig.permissionAudioCode, ByPermissionConfig.permissionAudio, view, new Comparable<String>() {
//                        @Override
//                        public int compareTo(String s) {
//                            jumpPermission(context, ByPermissionConfig.permissionCameraCode, ByPermissionConfig.permissionCamera, view, new Comparable<String>() {
//                                @Override
//                                public int compareTo(String s) {
//                                    comparable.compareTo("");
//                                    return 0;
//                                }
//                            });
//                            return 0;
//                        }
//                    });
//                } else if (requestCode == ByPermissionConfig.permissionAudioCode) {
//                    jumpPermission(context, ByPermissionConfig.permissionCameraCode, ByPermissionConfig.permissionCamera, view, new Comparable<String>() {
//                        @Override
//                        public int compareTo(String s) {
//                            comparable.compareTo("");
//                            return 0;
//                        }
//                    });
//                } else {
//                    comparable.compareTo("");
//                }
//            }
//
//            @Override
//            public void onPermissionsDenied(@NonNull List<String> perms) {
//        if (comparableClear != null) {
//                comparableClear.compareTo("");
//        }
//            }
//        });
    }

    /**
     * 存储及相机权限 申请及判断
     *
     * @param context
     * @param view
     * @param comparable
     */
    @SuppressLint("WrongConstant")
    public static void jumpPermissionCamera(Activity context, View view, Comparable<String> comparable) {
        if (ByPermissionTool.isPermission(context, ByPermissionConfig.permissionStorageAndCamera)) {
            comparable.compareTo("");
            return;
        }
        jumpPermission(context, ByPermissionConfig.permissionStorageAndCameraCode, ByPermissionConfig.permissionStorageAndCamera, view, new Comparable<String>() {
            @Override
            public int compareTo(String s) {
                comparable.compareTo("");
                return 0;
            }
        });
//        jumpPermission(context, ByPermissionConfig.permissionStorageCode, ByPermissionConfig.permissionStorage, view, new Comparable<String>() {
//            @Override
//            public int compareTo(String s) {
//                jumpPermission(context, ByPermissionConfig.permissionCameraCode, ByPermissionConfig.permissionCamera, view, new Comparable<String>() {
//                    @Override
//                    public int compareTo(String s) {
//                        comparable.compareTo("");
//                        return 0;
//                    }
//                });
//                return 0;
//            }
//        });
    }

    public static void jumpPermissionCameraResult(Activity context, int requestCode, String[] permissions, int[] grantResults, View view, Comparable<String> comparable) {
        jumpPermissionCameraResult(context, requestCode, permissions, grantResults, view, comparable, null);
    }

    public static void jumpPermissionCameraResult(Activity context, int requestCode, String[] permissions, int[] grantResults, View view, Comparable<String> comparable, Comparable<String> comparableDenied) {
        dismissTipPop();
        if (ByPermissionTool.isPermission(context, ByPermissionConfig.permissionStorageAndCamera)) {
            ByPermissionTool.onRequestPermissionsResult(context, permissions, grantResults, null);
            comparable.compareTo("");
            return;
        }
        if (comparableDenied != null) {
            comparableDenied.compareTo("");
        }
        dialogTip(context, permissions, grantResults);
//        ByPermissionTool.onRequestPermissionsResult(context, permissions, grantResults, new ByPermissionTool.PermissionCallback() {
//            @Override
//            public void onPermissionsGranted(@NonNull List<String> perms) {
//                if (requestCode == ByPermissionConfig.permissionStorageCode) {
//                    jumpPermission(context, ByPermissionConfig.permissionCameraCode, ByPermissionConfig.permissionCamera, view, new Comparable<String>() {
//                        @Override
//                        public int compareTo(String s) {
//                            comparable.compareTo("");
//                            return 0;
//                        }
//                    });
//                } else {
//                    comparable.compareTo("");
//                }
//            }
//
//            @Override
//            public void onPermissionsDenied(@NonNull List<String> perms) {
//                if (comparableDenied != null) {
//                    comparableDenied.compareTo("");
//                }
//            }
//        });
    }

    /**
     * 定位权限 申请及判断
     *
     * @param context
     * @param view
     * @param comparable
     */
    @SuppressLint("WrongConstant")
    public static void jumpPermissionLocation(Activity context, View view, Comparable<String> comparable) {
        jumpPermission(context, ByPermissionConfig.permissionLocationCode, ByPermissionConfig.permissionLocation, view, new Comparable<String>() {
            @Override
            public int compareTo(String s) {
                comparable.compareTo("");
                return 0;
            }
        });
    }

    public static void jumpPermissionLocationResult(Activity context, String[] permissions, int[] grantResults, PermissionResultComparator callback) {
        jumpPermissionResult(context, permissions, grantResults, callback);
    }

    public static void jumpPermissionResult(Activity context, String[] permissions, int[] grantResults, PermissionResultComparator callback) {
        dismissTipPop();
        ByPermissionTool.onRequestPermissionsResult(context, permissions, grantResults, new ByPermissionTool.PermissionCallback() {
            @Override
            public void onPermissionsGranted(@NonNull List<String> perms) {
                callback.permissionResult(perms);
            }

            @Override
            public void onPermissionsDenied(@NonNull List<String> perms) {
                dialogTip(context, permissions, grantResults);
            }
        });
    }

    /**
     * 权限申请
     *
     * @param context
     * @param code
     * @param permission
     * @param view
     * @param comparable
     */
    @SuppressLint("WrongConstant")
    public static void jumpPermission(Activity context, int code, String permission, View view, Comparable<String> comparable) {
        mExitTime = System.currentTimeMillis();
        int authorizeStatus = ByPermissionTool.getAuthorizeStatus(context, permission);
        if (authorizeStatus == ByPermissionTool.STATUS_SUCCESS) {
            if (comparable != null) {
                comparable.compareTo("");
            }
        } else if (authorizeStatus == ByPermissionTool.STATUS_DEFAULT || authorizeStatus == ByPermissionTool.STATUS_REFUSE) {
            if (permissionAuthReminderPop == null) {
                permissionAuthReminderPop = new PermissionAuthReminderPop(context);
            }
            permissionAuthReminderPop.showWin(view, permission);
            ActivityCompat.requestPermissions(context, new String[]{permission}, code);
        } else {
            dialogTip(context, getTip(permission));
        }
    }

    @SuppressLint("WrongConstant")
    public static void jumpPermission(Activity context, int code, String[] permissions, View view, Comparable<String> comparable) {
        mExitTime = System.currentTimeMillis();
        int authorizeStatus = ByPermissionTool.getAuthorizeStatus(context, permissions);
        if (authorizeStatus == ByPermissionTool.STATUS_SUCCESS) {
            if (comparable != null) {
                comparable.compareTo("");
            }
        } else if (authorizeStatus == ByPermissionTool.STATUS_DEFAULT || authorizeStatus == ByPermissionTool.STATUS_REFUSE) {
            if (permissionAuthReminderPop == null) {
                permissionAuthReminderPop = new PermissionAuthReminderPop(context);
            }
            permissionAuthReminderPop.showWin(view, permissions);
            ActivityCompat.requestPermissions(context, permissions, code);
        } else {
            dialogTip(context, permissions);
        }
    }

    /**
     * 权限请求返回操作
     *
     * @param permissions
     * @param grantResults
     */
    public static String onResultString(String[] permissions, int[] grantResults) {
        if (permissions == null) {
            return "";
        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < permissions.length; i++) {
            String perm = permissions[i];
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                String tip = getTip(perm);
                if (!str.toString().contains(tip)) {
                    str.append("[").append(tip).append("]、");
                }
            }
        }
        if (!TextUtils.isEmpty(str.toString())) {
            str = new StringBuilder(str.substring(0, str.length() - 1));
        }
        return str.toString();
    }

    public static String onResultString(Context context, String[] permissions) {
        if (permissions == null) {
            return "";
        }
        StringBuilder str = new StringBuilder();
        for (String perm : permissions) {
            if (ByPermissionTool.isPermission(context, perm)) {
                String tip = getTip(perm);
                if (!str.toString().contains(tip)) {
                    str.append("[").append(tip).append("]、");
                }
            }
        }
        if (!TextUtils.isEmpty(str.toString())) {
            str = new StringBuilder(str.substring(0, str.length() - 1));
        }
        return str.toString();
    }

    private static String getTip(String permission) {
        String str = "存储";
        if (permission.contains(("LOCATION"))) {
            str = "定位";
        } else if (permission.contains(("CAMERA"))) {
            str = "相机";
        } else if (permission.contains(("AUDIO"))) {
            str = "录音";
        } else if (permission.contains(("PHONE"))) {
            str = "设备信息";
        } else if (permission.contains(("Location"))) {
            str = "定位";
        }
        return str;
    }

    public static PermissionAuthReminderPop permissionAuthReminderPop;

    /**
     * 销毁权限提示界面
     */
    public static void dismissTipPop() {
        if (permissionAuthReminderPop != null) {
            permissionAuthReminderPop.dismiss();
            permissionAuthReminderPop = null;
        }
    }

    public static PermissionDeniedComparator comparator;

    public static void setPermissionDeniedComparator(PermissionDeniedComparator comparator) {
        ByPermissionHelper.comparator = comparator;
    }

    /**
     * 权限未处理弹窗
     *
     * @param context
     * @param permissions
     * @param grantResults
     */
    public static void dialogTip(Activity context, String[] permissions, int[] grantResults) {
        if (isExitStatus) {
            if ((System.currentTimeMillis() - mExitTime) > mExitTimeMillis) {
                mExitTime = System.currentTimeMillis();
                return;
            }
            dialogTip(context, onResultString(permissions, grantResults));
        }
    }

    public static void dialogTip(Activity context, String[] permissions) {
        if (isExitStatus) {
            if ((System.currentTimeMillis() - mExitTime) > mExitTimeMillis) {
                mExitTime = System.currentTimeMillis();
                return;
            }
            dialogTip(context, onResultString(context, permissions));
        }
    }

    public static void dialogTip(Activity context, String tip) {
        if (comparator != null) {
            comparator.permissionDenied(context, tip);
        }
    }

    /**
     * @param mExitTimeMillis 间隔时间
     */
    public static void setExitTimeMillis(long mExitTimeMillis) {
        ByPermissionHelper.mExitTimeMillis = mExitTimeMillis;
    }

    public static void setIsExitStatus(boolean isExitStatus) {
        ByPermissionHelper.isExitStatus = isExitStatus;
    }
}
